package org.example.xmut.qa.adapter.in.web.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.xmut.qa.adapter.in.web.config.MoonShotConfig;
import org.example.xmut.qa.adapter.in.web.dto.ChatCompletionRequestDTO;
import org.example.xmut.qa.adapter.in.web.dto.ChatCompletionResponseDTO;
import org.example.xmut.qa.adapter.in.web.dto.Choice;
import org.example.xmut.qa.adapter.in.web.dto.Message;
import org.example.xmut.qa.service.common.IdWorker;
import org.example.xmut.qa.service.domain.Qa;
import org.example.xmut.qa.service.domain.valueobject.Answer;
import org.example.xmut.qa.service.domain.valueobject.QaId;
import org.example.xmut.qa.service.domain.valueobject.Question;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Tag(name="示例控制器")
@RestController
@RequestMapping("kimi")
public class kimiController {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private MoonShotConfig moonShotConfig;

    @Resource
    private RabbitTemplate rabbitTemplate;

    // 创建IdWorker实例用于生成唯一ID
    private static final IdWorker idWorker = new IdWorker();

    @Operation(summary = "kimi示例")
    @GetMapping("demo")
    public List<Choice> demo(@RequestParam("question") String question) {
        log.info("demo");

        //创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + moonShotConfig.getKey());
        //创建请求体
        ChatCompletionRequestDTO request = new ChatCompletionRequestDTO();
        request.setModel(moonShotConfig.getModel());
        request.setTemperature(0.6);
        //创建消息列表
        Message systemMessage = new Message();
        systemMessage.setRole("system");
        systemMessage.setContent("你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长中文和英文的对话。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。");

        Message userMessage=new Message();
        userMessage.setRole("user");
        userMessage.setContent(question);

        request.setMessages(Arrays.asList(
                systemMessage,userMessage
        ));

        //创建Http实体
        HttpEntity<ChatCompletionResponseDTO> response= restTemplate.exchange(
                moonShotConfig.getUrl(),
                HttpMethod.POST,
                new HttpEntity<>(request, headers),
                ChatCompletionResponseDTO.class
        );

        log.info("response:{}",response);

        List<Choice> choices = response.getBody().getChoices();

        for (Choice choice : choices) {
            //把消息交给mq处理
            Qa qa = new Qa();
            // 使用IdWorker生成唯一ID
            qa.setId(new QaId(idWorker.nextId()));
            // 用户的原始问题
            qa.setQuestion(new Question(question));
            // Kimi的回答内容
            qa.setAnswer(new Answer(choice.getMessage().getContent()));
            rabbitTemplate.convertAndSend("qaTest",qa);
        }

        return choices;
    }
}
